$.views.converters({
    converterDecStatus : function(value) {
        if(!value){
            return "";
        }
        for(index in sysstatus_dict){
            var goodsStatus = sysstatus_dict[index];
            if(goodsStatus.dictCode == value){
                return goodsStatus.dictName;
            }
        }
        return  "未知类型("+value+")";
    }
});


var decManageCtrl  = new XFApp.client();
XFApp.client.prototype.decManage = {
    successCode : "10000",
    page : null,
    hisPage : null,
    hisId : 0,
    cusPage : null,
    reportId : null,
    urlItems : {
        //queryByPageURL : basePath+"/userCenter/goodRecord/queryPage",
        queryByPageURL : basePath+"/decManage/queryPage",
        //delGoodsRUL : basePath +"/userCenter/goods/goodsRecord/delGoods",
        delGoodsRUL : basePath +"/decManage/delDecHead",
        queryCustomerByPageURL : basePath + "/userCenter/goods/goodsRecord/queryCusPage",
        queryByHisPageURL : basePath + "/userCenter/goods/goodsStats/queryHistoryPage",
        uploadResultURL :basePath+"/userCenter/goods/goodsStats/uploadResult",
        exportExcelURL: basePath+"/userCenter/goods/excel/export",
        batchSubmitRUL: basePath+"/userCenter/goodRecord/submit",
        downLoadURL: basePath+"/userCenter/fileDownload/downloadTemplate"
    },

    queryPage : function(){
        _this = this;
        var params = _this.formatParam($('#query_from').serialize());
        var  pageindex = Number($('#decheadPageIndex').val());

        if(!!_this.page){

            _this.page.page("remote",0,params);
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                currentPageIndex:pageindex,
                remote: {
                    url: _this.urlItems.queryByPageURL,
                    params: params,
                    success: function (data, pageIndex) {
                        $('#decheadPageIndex').val(pageIndex);
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(pageResult.itemList.length>0){
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }else{
                                $("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }
                        }else{
                            webDialogUtil.simpleAlert(data.msg, "温馨提示");
                        }
                        _this.displayPop();
                    },
                    error : function(){
                        webDialogUtil.simpleAlert("数据查询异常，请联系开发人员", "温馨提示");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    displayPop:function(){
        $("[data-toggle='popover']").popover();
        $("[data-toggle='popover']").on('shown.bs.popover', function (event) {
            $("[data-toggle='popover']").each(function(){
                if(event.currentTarget.id!=$(this).attr("id")){
                    $(this).popover('hide');
                }
            });
        });
    },
    queryHisPage : function(id){
        _this = this;
        var params = {
            id :id
        }
        if(!!_this.hisPage){
            _this.hisPage.page("remote",0,params);
        }else{
            _this.hisPage = $("#history_page").page({
                pageSize : 5,
                remote: {
                    url: _this.urlItems.queryByHisPageURL,
                    params: params,
                    success: function (data, pageIndex) {
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(pageResult.itemList.length>0){
                                $("#his_page_grid").html($("#hisDataTmp").render(pageResult.itemList));
                            }else{
                                $("#his_page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }
                        }else{
                            webDialogUtil.simpleAlert(data.msg, "温馨提示");
                        }
                    },
                    error : function(){
                        webDialogUtil.simpleAlert("数据查询异常，请联系开发人员", "温馨提示");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
            _this.hisId = id;
        }
    },
    delGoods : function(id){
        webDialogUtil.simpleConfrimAlert('是否确认删除？', function(){
            $.ajax({
                url:_this.urlItems.delGoodsRUL,
                data:{id:id},//JSON.stringify(docket),
                dataType:'json',
                type:'post',
                success:function(data){
                    if(data.code!=10000){
                        webDialogUtil.simpleAlert(data.msg, "温馨提示");
                    }else{
                        _this.queryPage();
                    }
                },
                error:function(){
                    webDialogUtil.simpleAlert("删除出现异常,请联系开发人员！", "温馨提示");
                }
            });
        });
    },
    selCustomer : function(value){
        $("#customercode").val(value);
        $("#choose_customer_modal").modal('hide');
    },
    queryHistory : function(id){
        _this.queryHisPage(id);
        $("#choose_history_modal").modal('show');
    },
    uploadModal : function(id,path){
        this.reportId = id;
        if(!!path){
            webDialogUtil.simpleConfrimAlert("改商品已上传过检测报告，是否更新报告？", function(){
                $("#checkResult").val("");
                $("#upload_modal").modal('show');
            });
        }else{
            $("#checkResult").val("");
            $("#upload_modal").modal('show');
        }

    },
    formatParam: function(param){
        var newParam = param.replace(/\+/g," ");
        newParam = decodeURIComponent(newParam);
        return newParam;
    },
    downloadTemplate: function(fileName){
        _this = this;
        var url = _this.urlItems.downLoadURL+"?fileName="+fileName;
        var iframeId = "goods_tmpfile_download_frame" ;
        $("body").append("<iframe id='"+iframeId+"' src='" + url +"' frameborder=0 width=0 height=0 style='display:none'></iframe>");
    },
    exportExcel : function(){
        var _this = this;
        var params = [];
        $("#query_from input").each(function() {
            if(!!$(this).attr("name")){
                var param = {name:'',value:''};
                param.name=$(this).attr("name");
                param.value= $(this).val();
                params.push(param);
            }
        });
        $("#query_from select").each(function() {
            if(!!$(this).attr("name")){
                var param = {name:'',value:''};
                param.name=$(this).attr("name");
                param.value= $(this).val();
                params.push(param);
            }
        });
        webDialogUtil.simpleConfrimAlert("是否确认导出", function(){
            $.ajaxFileUpload({
                url : _this.urlItems.exportExcelURL,
                secureuri:false,
                dataType: 'json',
                data : params,
                success: function (data, status) {
                    if(data.code!=10000){
                        webDialogUtil.simpleAlert(data.msg, "温馨提示");
                    }
                },
                error: function (data, status, e) {
                    webDialogUtil.simpleAlert("处理异常", "温馨提示");
                }
            });
        });
    },
    selectAllRow: function(){
        if($('#selectAll').is(':checked')){
            $(".table-responsive input[type='checkbox']").each(function(index,el){
                if(el.id != 'selectAll'){
                    el.checked = true;
                }
            });
        }else{
            $(".table-responsive input[type='checkbox']").each(function(index,el){
                if(el.id != 'selectAll'){
                    el.checked = false;
                }
            });
        }
    },
    batchSubmit: function(){
        var ids = '';
        $(".table-responsive input[type='checkbox']").each(function(index,el){
            if(el.id != 'selectAll' && el.checked){
                ids += el.id + ','
            }
        });
        if(ids == ''){
            webDialogUtil.simpleAlert("请选择需要提交的记录", "温馨提示");
            return;
        }
        ids = ids.substring(0,ids.length-1);
        var num = ids.split(',').length
        webDialogUtil.simpleConfrimAlert("是否确认提交选中的"+num+"条记录", function(){
            $.ajax({
                url:_this.urlItems.batchSubmitRUL,
                data:{ids:ids},//JSON.stringify(docket),
                dataType:'json',
                type:'post',
                success:function(data){
                    if(data.code!=10000){
                        webDialogUtil.simpleAlert(data.msg, "温馨提示");
                    }else{
                        webDialogUtil.simpleAlert('提交成功', "温馨提示");
                        _this.queryPage();
                    }
                },
                error:function(){
                    webDialogUtil.simpleAlert("提交异常,请联系开发人员cccc！", "温馨提示");
                }
            });
        });
    },
    upload: function(){
        var _this = this;
        var params = [];
        var param = {
            name:"id",
            value:_this.reportId
        }
        params.push(param);
        webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
            $.ajaxFileUpload({
                url : _this.urlItems.uploadResultURL,
                secureuri:false,
                fileElementId:'checkResult',//file标签的id
                dataType: 'json',
                data : params,
                success: function (data, status) {
                    if(data.code!=10000){
                        webDialogUtil.simpleAlert(data.msg, "温馨提示");
                    }else{
                        $("#upload_modal").modal('hide');
                        webDialogUtil.simpleAlert("处理成功", "温馨提示");
                    }
                },
                error: function (data, status, e) {
                    webDialogUtil.simpleAlert("处理异常", "温馨提示");
                }
            });
        });
    },
    event : function(){
        _this = this;

        $("#downloadButton2").on("click", function(){
            decManageCtrl.decManage.downloadTemplate('goodsTemplate.xls');
        });
        $("#goods_search").on("click",function(){
            _this.queryPage();
        });
        $("#customercode_choose").on("click",function(){
            $("#choose_customer_modal").modal();
        });
        $("#custom_serach").on("click",function(){
            _this.queryCusPage();
        });
        $("#custom_reset").on("click",function(){
            $("input[name='customercodeFilter']").val("");
            $("input[name='customernameFilter']").val("");
        });
        $('#selectAll').on('change',function(){
            _this.selectAllRow();
        });

        $('#batchSubBtn').on('click',function(){
            _this.batchSubmit();
        });
    }
};
$(function(){
    decManageCtrl.decManage.event();
    decManageCtrl.decManage.queryPage();
});
